Ugly number II [BST, Heap]¶
Time: O(N); Space: O(1); medium
Write a program to find the n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
Example 1:
Input: n = 10
Output: 12
Explanation:
1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
Example 2:
Input: 1
Output: 1
Notes:
1 is typically treated as an ugly number.
n does not exceed 1690.
Challenge:
O(NxLogN) or O(N) time.
Hints:
The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.
An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3.
Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 * 2, L2 * 3, L3 * 5).
1. Heap¶
[12]:
import heapq
class Solution1(object):
"""
Time: O(N)
Space: O(1)
"""
def nthUglyNumber(self, n):
"""
:type n: int
:rtype: int
"""
ugly_number = 0
heap = []
heapq.heappush(heap, 1)
for _ in range(n):
ugly_number = heapq.heappop(heap)
if ugly_number % 2 == 0:
heapq.heappush(heap, ugly_number * 2)
elif ugly_number % 3 == 0:
heapq.heappush(heap, ugly_number * 2)
heapq.heappush(heap, ugly_number * 3)
else:
heapq.heappush(heap, ugly_number * 2)
heapq.heappush(heap, ugly_number * 3)
heapq.heappush(heap, ugly_number * 5)
return ugly_number
[13]:
s = Solution1()
n = 10
assert s.nthUglyNumber(n) == 12
n = 1
assert s.nthUglyNumber(n) == 1
2. Heap¶
[14]:
import heapq
class Solution2(object):
def nthUglyNumber(self, n):
"""
:type n: int
:rtype: int
"""
q2, q3, q5 = [2], [3], [5]
ugly = 1
for u in heapq.merge(q2, q3, q5):
if n == 1:
return ugly
if u > ugly:
ugly = u
n -= 1
q2 += 2 * u,
q3 += 3 * u,
q5 += 5 * u,
[15]:
s = Solution2()
n = 10
assert s.nthUglyNumber(n) == 12
n = 1
assert s.nthUglyNumber(n) == 1
3.¶
[16]:
class Solution3(object):
def nthUglyNumber(self, n):
"""
:type n: int
:rtype: int
"""
ugly = sorted(2**a * 3**b * 5**c
for a in range(32) for b in range(20) for c in range(14))
return ugly[n-1]
[17]:
s = Solution3()
n = 10
assert s.nthUglyNumber(n) == 12
n = 1
assert s.nthUglyNumber(n) == 1
4.¶
[18]:
class Solution4(object):
def nthUglyNumber(self, n):
"""
:type n: int
:rtype: int
"""
ugly = [1]
i2 = i3 = i5 = 0
while len(ugly) < n:
while ugly[i2] * 2 <= ugly[-1]:
i2 += 1
while ugly[i3] * 3 <= ugly[-1]:
i3 += 1
while ugly[i5] * 5 <= ugly[-1]:
i5 += 1
ugly.append(min(ugly[i2] * 2, ugly[i3] * 3, ugly[i5] * 5))
return ugly[-1]
[19]:
s = Solution4()
n = 10
assert s.nthUglyNumber(n) == 12
n = 1
assert s.nthUglyNumber(n) == 1
Related problems:¶
https://leetcode.com/problems/count-primes/
https://leetcode.com/problems/ugly-number-iii/
https://www.lintcode.com/problem/merge-k-sorted-lists
https://www.lintcode.com/problem/narcissistic-number
https://www.lintcode.com/problem/happy-number
https://www.lintcode.com/problem/perfect-squares